package com.spring.springboot.controller;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.springboot.entity.UserEntity;
import com.spring.springboot.jpa.UserJPA;

import lombok.experimental.Wither;

/**
 * ========================
 * Created with IntelliJ IDEA.
 * User：恒宇少年
 * Date：2017/4/7
 * Time：21:41
 * 码云：http://git.oschina.net/jnyqy
 * ========================
 */
@Controller
@RequestMapping("/user")
public class IndexController {

	@Autowired
	private UserJPA userJPA ;
    /**
     * 初始化登录页面
     * @return
     */
    @RequestMapping(value = "/login_view",method = RequestMethod.GET)
    public String login_view(){
        return "login";
    }

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "index";
    }
    
    @SuppressWarnings("deprecation")
    @GetMapping("/cutpage")
    @ResponseBody
	public List<UserEntity> cutPage(int page){
    	UserEntity user = new UserEntity();
    	user.setPage(page);
    	user.setSize(2);
    	user.setSord("asc");
    	user.setSidx("age");
    	Sort.Direction sort_direction = Sort.Direction.ASC.toString().equalsIgnoreCase(user.getSord()) ? Sort.Direction.ASC : Sort.Direction.DESC;
    	
    	Sort sort = new Sort(sort_direction, user.getSidx());
    	
    	PageRequest pageRequest = new PageRequest(user.getPage() - 1, user.getSize(), sort);
    	return userJPA.findAll(pageRequest).getContent();
    	
    }
    
    @SuppressWarnings("deprecation")
    @GetMapping("/cutpage1")
    @ResponseBody
	public List<UserEntity> cutPage1(int page, BigDecimal balance){
    	UserEntity user = new UserEntity();
    	user.setPage(page);
    	user.setSize(2);
    	user.setSord("asc");
    	user.setSidx("age");
    	Sort.Direction sort_direction = Sort.Direction.ASC.toString().equalsIgnoreCase(user.getSord()) ? Sort.Direction.ASC : Sort.Direction.DESC;
    	
    	Sort sort = new Sort(sort_direction, user.getSidx());
    	Pageable pageable = new PageRequest(user.getPage() - 1, user.getSize(), sort);
    	return userJPA.findAllByBalance(balance, pageable);
    	
    }
    
    
}
